from PySide6 import QtGui
from PySide6.QtWidgets import (
    QWidget,
    QLabel,
    QLineEdit,
    QPushButton,
    QVBoxLayout,
    QHBoxLayout,
    QMessageBox, QGraphicsDropShadowEffect,
)


class FloatToLatLon(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setWindowTitle("浮点数转经纬度")
        self.setObjectName("浮点数转经纬度")

        self.label_lon = QLabel("经度：")
        self.input_lon = QLineEdit()
        self.input_lon.setPlaceholderText("120.5555")

        self.label_lat = QLabel("纬度：")
        self.input_lat = QLineEdit()
        self.input_lat.setPlaceholderText("55.5555")

        self.button_conv = QPushButton("转换")
        self.button_clear = QPushButton("清空")
        # 创建阴影效果并应用于按钮
        shadow = QGraphicsDropShadowEffect()
        shadow.setBlurRadius(50)
        shadow.setColor(QtGui.QColor(0, 0, 0, 160))
        self.button_conv.setGraphicsEffect(shadow)

        layout = QVBoxLayout()

        lon_layout = QHBoxLayout()
        lon_layout.addWidget(self.label_lon)
        lon_layout.addWidget(self.input_lon)

        lat_layout = QHBoxLayout()
        lat_layout.addWidget(self.label_lat)
        lat_layout.addWidget(self.input_lat)

        button_layout = QHBoxLayout()
        button_layout.addWidget(self.button_conv)
        button_layout.addWidget(self.button_clear)

        layout.addLayout(lon_layout)
        layout.addLayout(lat_layout)
        layout.addLayout(button_layout)

        self.setLayout(layout)

        self.button_conv.clicked.connect(self.convert)
        self.button_clear.clicked.connect(self.clear)

    def convert(self):
        """
        :return:
        """
        try:
            lon = float(self.input_lon.text())
            lat = float(self.input_lat.text())
        except ValueError:
            self.msg_box(QMessageBox.Icon.Warning, "警告", "请输入正确的经纬度值")
            return

        if abs(lon) > 180 or abs(lat) > 90:
            self.msg_box(QMessageBox.Icon.Warning, "警告", "请输入正确的经纬度值")
            return
        else:
            __lon = self.decimal_to_base60(lon, False)
            __lat = self.decimal_to_base60(lat, True)
            self.msg_box(
                QMessageBox.Icon.Information,
                "转换结果",
                f"经度：{__lon[0]}°{__lon[1]}′{__lon[2]}″{__lon[3]}\n"
                f"纬度：{__lat[0]}°{__lat[1]}′{__lat[2]}″{__lat[3]}\n",
            )

    def msg_box(self, msg_type: QMessageBox.Icon, title: str, message: str):
        """

        :param msg_type:
        :param title:
        :param message:
        :return:
        """
        msg_box = QMessageBox(self)
        msg_box.setIcon(msg_type)
        msg_box.setText(message)
        msg_box.setWindowTitle(title)
        msg_box.exec()

    @staticmethod
    def decimal_to_base60(decimal_number: float, is_latitude: float):
        """
        十进制转度分秒
        :param decimal_number:
        :param is_latitude:  是否纬度
        :return:
        """
        if is_latitude:
            if decimal_number < 0:
                direction = "S"
                decimal_number = abs(decimal_number)
            else:
                direction = "N"
        else:
            if decimal_number < 0:
                direction = "W"
                decimal_number = abs(decimal_number)
            else:
                direction = "E"

        degrees = int(decimal_number)
        minutes_float = (decimal_number - degrees) * 60
        minutes = int(minutes_float)
        seconds = (minutes_float - minutes) * 60

        return degrees, minutes, seconds, direction

    def clear(self):
        self.input_lon.clear()
        self.input_lat.clear()
